package com.lin.open.server.modules.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.lin.open.cache.core.constants.CacheConstants;
import com.lin.open.core.constants.SystemConstants;
import com.lin.open.core.response.R;
import com.lin.open.server.modules.user.constants.UserConstants;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;

@RestController
@Api(tags = "测试sa-token")
@RequestMapping("/")
@Slf4j
public class TestController {

    // 测试登录  ---- http://localhost:8081/acc/doLogin?name=zhang&pwd=123456
    @GetMapping("doLogin")
    public R doLogin(String name, String pwd) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("zhang".equals(name) && "123456".equals(pwd)) {
            StpUtil.login(10001);
            return R.success("登录成功");
        }
        return R.fail("登录失败");
    }

    // 查询登录状态  ---- http://localhost:8081/acc/isLogin
    @GetMapping("isLogin")
    public R isLogin() {
        return R.success("是否登录：" + StpUtil.isLogin());
    }

    // 查询 Token 信息  ---- http://localhost:8081/acc/tokenInfo
    @GetMapping("tokenInfo")
    public R tokenInfo() {
        return R.data(StpUtil.getTokenInfo());
    }

    // 查询 Token 信息  ---- http://localhost:8081/acc/tokenInfo
    @GetMapping("getIdByToken")
    public R getIdByToken() {
        Serializable id = (Serializable) StpUtil.getLoginIdByToken(StpUtil.getTokenValue());
        return R.data(id);
    }

    // 测试注销  ---- http://localhost:8081/acc/logout
    @GetMapping("logout")
    public R logout() {
        StpUtil.logout();
        return R.success();
    }
}
